package org.htmlparser.tags;

import java.util.Locale;
import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Tag;
import org.htmlparser.Text;
import org.htmlparser.filters.NodeClassFilter;
import org.htmlparser.lexer.Page;
import org.htmlparser.nodes.AbstractNode;
import org.htmlparser.nodes.TagNode;
import org.htmlparser.scanners.CompositeTagScanner;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.SimpleNodeIterator;
import org.htmlparser.visitors.NodeVisitor;

public class CompositeTag extends TagNode
{
  protected static final CompositeTagScanner mDefaultCompositeScanner = new CompositeTagScanner();
  protected Tag mEndTag;

  public CompositeTag()
  {
    setThisScanner(mDefaultCompositeScanner);
  }

  public void accept(NodeVisitor paramNodeVisitor)
  {
    if (paramNodeVisitor.shouldRecurseSelf())
      paramNodeVisitor.visitTag(this);
    if (paramNodeVisitor.shouldRecurseChildren())
    {
      if (getChildren() != null)
      {
        SimpleNodeIterator localSimpleNodeIterator = children();
        while (localSimpleNodeIterator.hasMoreNodes())
          localSimpleNodeIterator.nextNode().accept(paramNodeVisitor);
      }
      if ((getEndTag() != null) && (this != getEndTag()))
        getEndTag().accept(paramNodeVisitor);
    }
  }

  public Node childAt(int paramInt)
  {
    Node localNode;
    if (getChildren() == null)
      localNode = null;
    while (true)
    {
      return localNode;
      localNode = getChildren().elementAt(paramInt);
    }
  }

  public SimpleNodeIterator children()
  {
    SimpleNodeIterator localSimpleNodeIterator;
    if (getChildren() != null)
      localSimpleNodeIterator = getChildren().elements();
    while (true)
    {
      return localSimpleNodeIterator;
      localSimpleNodeIterator = new NodeList().elements();
    }
  }

  public void collectInto(NodeList paramNodeList, NodeFilter paramNodeFilter)
  {
    super.collectInto(paramNodeList, paramNodeFilter);
    SimpleNodeIterator localSimpleNodeIterator = children();
    while (localSimpleNodeIterator.hasMoreNodes())
      localSimpleNodeIterator.nextNode().collectInto(paramNodeList, paramNodeFilter);
    if ((getEndTag() != null) && (this != getEndTag()))
      getEndTag().collectInto(paramNodeList, paramNodeFilter);
  }

  public Text[] digupStringNode(String paramString)
  {
    NodeList localNodeList1 = searchFor(paramString);
    NodeList localNodeList2 = new NodeList();
    int i = 0;
    if (i < localNodeList1.size())
    {
      Node localNode = localNodeList1.elementAt(i);
      if (localNode instanceof Text)
        localNodeList2.add(localNode);
      do
        while (true)
          ++i;
      while (!(localNode instanceof CompositeTag));
      Text[] arrayOfText2 = ((CompositeTag)localNode).digupStringNode(paramString);
      int k = 0;
      while (true)
      {
        if (k >= arrayOfText2.length);
        localNodeList2.add(arrayOfText2[k]);
        ++k;
      }
    }
    Text[] arrayOfText1 = new Text[localNodeList2.size()];
    for (int j = 0; j < arrayOfText1.length; ++j)
      arrayOfText1[j] = ((Text)localNodeList2.elementAt(j));
    return arrayOfText1;
  }

  public SimpleNodeIterator elements()
  {
    SimpleNodeIterator localSimpleNodeIterator;
    if (getChildren() == null)
      localSimpleNodeIterator = new NodeList().elements();
    while (true)
    {
      return localSimpleNodeIterator;
      localSimpleNodeIterator = getChildren().elements();
    }
  }

  public int findPositionOf(String paramString)
  {
    return findPositionOf(paramString, Locale.ENGLISH);
  }

  public int findPositionOf(String paramString, Locale paramLocale)
  {
    int j;
    int i = 0;
    String str = paramString.toUpperCase(paramLocale);
    SimpleNodeIterator localSimpleNodeIterator = children();
    if (localSimpleNodeIterator.hasMoreNodes())
      if (-1 != localSimpleNodeIterator.nextNode().toPlainTextString().toUpperCase(paramLocale).indexOf(str))
        j = i;
    while (true)
    {
      while (true)
      {
        return j;
        ++i;
      }
      j = -1;
    }
  }

  public int findPositionOf(Node paramNode)
  {
    int j;
    int i = 0;
    SimpleNodeIterator localSimpleNodeIterator = children();
    if (localSimpleNodeIterator.hasMoreNodes())
      if (localSimpleNodeIterator.nextNode() == paramNode)
        j = i;
    while (true)
    {
      while (true)
      {
        return j;
        ++i;
      }
      j = -1;
    }
  }

  public Node getChild(int paramInt)
  {
    Node localNode;
    if (getChildren() == null)
      localNode = null;
    while (true)
    {
      return localNode;
      localNode = getChildren().elementAt(paramInt);
    }
  }

  public int getChildCount()
  {
    int i;
    NodeList localNodeList = getChildren();
    if (localNodeList == null)
      i = 0;
    while (true)
    {
      return i;
      i = localNodeList.size();
    }
  }

  public Node[] getChildrenAsNodeArray()
  {
    Node[] arrayOfNode;
    if (getChildren() == null)
      arrayOfNode = new Node[0];
    while (true)
    {
      return arrayOfNode;
      arrayOfNode = getChildren().toNodeArray();
    }
  }

  public String getChildrenHTML()
  {
    StringBuffer localStringBuffer = new StringBuffer();
    SimpleNodeIterator localSimpleNodeIterator = children();
    while (localSimpleNodeIterator.hasMoreNodes())
      localStringBuffer.append(((AbstractNode)localSimpleNodeIterator.nextNode()).toHtml());
    return localStringBuffer.toString();
  }

  public Tag getEndTag()
  {
    return this.mEndTag;
  }

  public String getStringText()
  {
    int i = getEndPosition();
    int j = this.mEndTag.getStartPosition();
    return getPage().getText(i, j);
  }

  public String getText()
  {
    String str = super.toHtml(true);
    return str.substring(1, str.length() - 1);
  }

  protected void putChildrenInto(StringBuffer paramStringBuffer, boolean paramBoolean)
  {
    SimpleNodeIterator localSimpleNodeIterator = children();
    while (true)
    {
      Node localNode;
      do
      {
        if (!(localSimpleNodeIterator.hasMoreNodes()))
          return;
        localNode = localSimpleNodeIterator.nextNode();
      }
      while ((paramBoolean) && (localNode.getStartPosition() == localNode.getEndPosition()));
      paramStringBuffer.append(localNode.toHtml());
    }
  }

  protected void putEndTagInto(StringBuffer paramStringBuffer, boolean paramBoolean)
  {
    if ((!(paramBoolean)) || (this.mEndTag.getStartPosition() != this.mEndTag.getEndPosition()))
      paramStringBuffer.append(getEndTag().toHtml());
  }

  public void removeChild(int paramInt)
  {
    if (getChildren() != null)
      getChildren().remove(paramInt);
  }

  public Tag searchByName(String paramString)
  {
    label76: Tag localTag2;
    Tag localTag1 = null;
    int i = 0;
    SimpleNodeIterator localSimpleNodeIterator = children();
    while (true)
    {
      String str;
      do
      {
        Node localNode;
        do
        {
          if ((!(localSimpleNodeIterator.hasMoreNodes())) || (i != 0))
            break label76;
          localNode = localSimpleNodeIterator.nextNode();
        }
        while (!(localNode instanceof Tag));
        localTag1 = (Tag)localNode;
        str = localTag1.getAttribute("NAME");
      }
      while ((str == null) || (!(str.equals(paramString))));
      i = 1;
    }
    if (i != 0)
      localTag2 = localTag1;
    while (true)
    {
      return localTag2;
      localTag2 = null;
    }
  }

  public NodeList searchFor(Class paramClass, boolean paramBoolean)
  {
    NodeList localNodeList2;
    NodeList localNodeList1 = getChildren();
    if (localNodeList1 == null)
      localNodeList2 = new NodeList();
    while (true)
    {
      return localNodeList2;
      localNodeList2 = localNodeList1.extractAllNodesThatMatch(new NodeClassFilter(paramClass), paramBoolean);
    }
  }

  public NodeList searchFor(String paramString)
  {
    return searchFor(paramString, false);
  }

  public NodeList searchFor(String paramString, boolean paramBoolean)
  {
    return searchFor(paramString, paramBoolean, Locale.ENGLISH);
  }

  public NodeList searchFor(String paramString, boolean paramBoolean, Locale paramLocale)
  {
    NodeList localNodeList = new NodeList();
    if (!(paramBoolean))
      paramString = paramString.toUpperCase(paramLocale);
    SimpleNodeIterator localSimpleNodeIterator = children();
    while (true)
    {
      Node localNode;
      String str;
      do
      {
        if (!(localSimpleNodeIterator.hasMoreNodes()))
          break label86;
        localNode = localSimpleNodeIterator.nextNode();
        str = localNode.toPlainTextString();
        if (!(paramBoolean))
          str = str.toUpperCase(paramLocale);
      }
      while (-1 == str.indexOf(paramString));
      localNodeList.add(localNode);
    }
    label86: return localNodeList;
  }

  public void setEndTag(Tag paramTag)
  {
    this.mEndTag = paramTag;
  }

  public String toHtml(boolean paramBoolean)
  {
    StringBuffer localStringBuffer = new StringBuffer();
    localStringBuffer.append(super.toHtml(paramBoolean));
    if (!(isEmptyXmlTag()))
    {
      putChildrenInto(localStringBuffer, paramBoolean);
      if (getEndTag() != null)
        putEndTagInto(localStringBuffer, paramBoolean);
    }
    return localStringBuffer.toString();
  }

  public String toPlainTextString()
  {
    StringBuffer localStringBuffer = new StringBuffer();
    SimpleNodeIterator localSimpleNodeIterator = children();
    while (localSimpleNodeIterator.hasMoreNodes())
      localStringBuffer.append(localSimpleNodeIterator.nextNode().toPlainTextString());
    return localStringBuffer.toString();
  }

  public String toString()
  {
    StringBuffer localStringBuffer = new StringBuffer(1024);
    toString(0, localStringBuffer);
    return localStringBuffer.toString();
  }

  public void toString(int paramInt, StringBuffer paramStringBuffer)
  {
    for (int i = 0; i < paramInt; ++i)
      paramStringBuffer.append("  ");
    paramStringBuffer.append(super.toString());
    paramStringBuffer.append(System.getProperty("line.separator"));
    SimpleNodeIterator localSimpleNodeIterator = children();
    while (true)
    {
      Node localNode;
      while (true)
      {
        if (!(localSimpleNodeIterator.hasMoreNodes()))
          break label129;
        localNode = localSimpleNodeIterator.nextNode();
        if (!(localNode instanceof CompositeTag))
          break;
        ((CompositeTag)localNode).toString(paramInt + 1, paramStringBuffer);
      }
      for (int k = 0; k <= paramInt; ++k)
        paramStringBuffer.append("  ");
      paramStringBuffer.append(localNode);
      paramStringBuffer.append(System.getProperty("line.separator"));
    }
    if ((getEndTag() != null) && (this != getEndTag()))
    {
      for (int j = 0; j <= paramInt; ++j)
        label129: paramStringBuffer.append("  ");
      paramStringBuffer.append(getEndTag().toString());
      paramStringBuffer.append(System.getProperty("line.separator"));
    }
  }
}